package com.dcbeida.fragment.stockFragment;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;

import com.dcbeida.MyApplication;
import com.dcbeida.bean.Stock;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.model.ModelUtils;
import com.dcbeida.utils.JsonGet;

import java.lang.reflect.Type;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by 150410 on 2017/9/8.
 */

public class StockPersenter implements StockContract.Persent {

    private final StockContract.View view;
    private final Activity act;
    private final ModelUtils model;

    public StockPersenter(StockContract.View view, Activity act) {
        this.view = view;
        this.act = act;
        model = ModelUtils.newInstance();
    }

    @Override
    public void start() {

    }


    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };


    @Override
    public void getList(int pageindex, int pagesize) {
        getList(pageindex, pagesize, true);
    }

    @Override
    public void getList(int pageindex, int pagesize, boolean isShow) {
        JsonGet jg = new JsonGet().putopt("uid", MyApplication.user.getUid())
                .putopt("shopid", MyApplication.user.getShopid())
                .putopt("pageindex", pageindex)
                .putopt("pagesize", pagesize);

        String json = jg.tostring();

        model.getDate("getstocksummaryv2", json, new GetListCallBack(act, Stock.class, lis, isShow));
    }

    public class GetListCallBack<T> extends DialogCallback<T> {
        public GetListCallBack(Activity activity, Type type, DialogInterface.OnCancelListener lis, boolean isShow) {
            super(activity, type, lis,isShow);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (((Stock) data).getList().size() >= 0) {
                    view.showList(((Stock) data).getList());
                    view.showData(((Stock) data));
                }
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showView(false);
            view.showTelTip("数据未请求成功,请检查网络...");
        }
    }

}
